package spy;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class DLogTest {
  @Test
  public void writesEachMessageToAllTargets() throws Exception {
    SpyTarget spy1 = new SpyTarget();
    SpyTarget spy2 = new SpyTarget();
    DLog log = new DLog(spy1, spy2);
    log.write(Level.INFO, "message");
    assertTrue(spy1.received(Level.INFO, "message"));
    assertTrue(spy2.received(Level.INFO, "message"));
  }

  private class SpyTarget implements DLogTarget {
    private List<String> log = new ArrayList<String>();
    public void write(Level level, String message) {
      log.add(concatenated(level, message));
    }

    boolean received(Level level, String message) {
      return log.contains(concatenated(level, message));
    }

    private String concatenated(Level level, String message) {
      return level + ": " + message;
    }
  }
}
